package com.gexin.service.conpon;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.IdGenerateUtils;
import com.gexin.common.utils.OtherUtil;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.activity.Coupon;
import com.gexin.model.integral.UserIntegralRecord;
import com.gexin.model.user.UserCoupon;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

/**
 * 优惠券服务
 */
public class CouponService {
	public static CouponService dao = new CouponService();

	/**
	 * 获取所有的优惠券活动
	 * @param getpage
	 * @param getpageSize
	 */
	public Page<Coupon> findList(int page , int pageSize) {
		return Coupon.dao.findList(page,pageSize);
	}

	public Page<Coupon> findAllListByUserId(int pageIndex , int pageSize , WeixinUser authUser , String state , String type) {
		return Coupon.dao.findAllListByUserId(pageIndex,pageSize,authUser.getId()+"",state,type);
	}

	public ApiResponse receiveCoupon(String couponId , WeixinUser authUser) {
		Coupon coupon = Coupon.dao.findById(couponId);
		if(coupon==null){
			return new ApiResponse(false,"优惠券不存在!");
		}
		boolean minuteBetween = OtherUtil.hourMinuteBetween(new Date() , coupon.getStartTime() , coupon.getEndTime());
		if(!minuteBetween){
			return new ApiResponse(false,"已超过领取时间!");
		}
		UserCoupon userCoupons = UserCoupon.dao.findByUserIdAndCoupon(authUser.getId() , coupon.getId());
		if(userCoupons!=null){
			return new ApiResponse(false,"已经领取该优惠券!不要重复领取!");
		}
		if(coupon.getQuota()==null){
			coupon.setQuota(0);
		}
		if(coupon.getTakeCount()==null){
			coupon.setTakeCount(0);
		}
		if(coupon.getQuota()-coupon.getTakeCount()<=0){
			return new ApiResponse(false,"该优惠券已经领取完毕!");
		}
		if(authUser.getSumIntegral()-coupon.getIntegral()<=0){
			return new ApiResponse(false,"用户积分不足!");
		}
		userCoupons = new UserCoupon();
		userCoupons.setCreatetime(new Date());
		userCoupons.setUserId(authUser.getId());
		userCoupons.setCouponNo(couponId);
		userCoupons.setFaceAmount(coupon.getUsedAmount());
		userCoupons.setRuleMoney(coupon.getWithAmount());
		userCoupons.setUseDate(coupon.getValidStartTime());
		userCoupons.setEndingDate(coupon.getValidEndTime());
		userCoupons.setUseState(0);
		userCoupons.setId(IdGenerateUtils.getId());
		boolean save = userCoupons.save();
		if(save){
			coupon.setQuota(coupon.getQuota()-1);
			coupon.setTakeCount(coupon.getTakeCount()+1);
			coupon.update();
			UserIntegralRecord.dao.saveLog(coupon.getIntegral(),UserIntegralRecord.TYPESUB,"兑换积分卷",authUser.getId());
			authUser.setSumIntegral(authUser.getSumIntegral()-coupon.getIntegral());
			authUser.update();
		}
		return new ApiResponse(save,save?"领取成功!":"领取失败!");

	}

	/**
	 * 根据id删除优惠券
	 * @param id 优惠券id
	 */
	public ApiResponse deleteCouponById(String id) {
		if(StringUtil.isBlank(id)){
			return new ApiResponse(false,"id不能为null!");
		}
		List<UserCoupon> list = UserCoupon.dao.findListByCoupon(id);
		if(list!=null&&list.size()>0){
			return new ApiResponse(false,"已有用户领取该优惠券!不能删除!");
		}
		boolean delete = Coupon.dao.deleteById(id);
		return new ApiResponse(delete,delete?"删除成功!":"删除失败!");
	}



}
